﻿using JNPF.Basics.Models;
using JNPF.Basics.Models.PlatForm.Dtos.CacheManage;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 缓存管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.08.01
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class CacheManageController : BaseApiController
    {
        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResult GetList([FromQuery] KeywordInput input)
        {
            var tenantId = TenantProvider.GetTenant.TenantId;
            var data = new List<CacheModel>();
            var keys = CacheFactory.Cache.GetAllKeys();
            if (!string.IsNullOrEmpty(tenantId))
                keys = keys.FindAll(q => q.Contains(tenantId));
            foreach (var key in keys)
            {
                var model = new CacheModel();
                model.name = key;
                model.overdueTime = CacheFactory.Cache.GetLiveTime(model.name);
                data.Add(model);
            }
            if (!input.keyword.IsNullOrEmpty())
            {
                data = data.FindAll(x => x.name.Contains(input.keyword));
            }
            return Success(new { list = data.OrderBy(o => o.overdueTime) });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="name">缓存名称</param>
        [HttpGet("{name}")]
        public ResponseResult GetInfo(string name)
        {
            try
            {
                var strJson = CacheFactory.Cache.Get(name);
                var cacheManageInfoOutput = new CacheManageInfoOutput();
                cacheManageInfoOutput.name = name;
                cacheManageInfoOutput.value = strJson;
                return Success("Success", cacheManageInfoOutput);
            }
            catch (Exception)
            {
                return Error("读取失败");
            }
        }
        #endregion

        #region POST
        /// <summary>
        /// 清空单个缓存
        /// </summary>
        /// <param name="name">name</param>
        /// <returns></returns>
        [HttpDelete("{name}")]
        public ResponseResult Clear(string name)
        {
            CacheFactory.Cache.Remove(name);
            return Success("清理成功");
        }

        /// <summary>
        /// 清空所有缓存
        /// </summary>
        /// <returns></returns>
        [HttpPost("Actions/ClearAll")]
        public ResponseResult ClearAll()
        {
            var tenantId = TenantProvider.GetTenant.TenantId;
            var keys = CacheFactory.Cache.GetAllKeys();
            if (!string.IsNullOrEmpty(tenantId))
                keys = keys.FindAll(q => q.Contains(tenantId));
            foreach (var key in keys)
            {
                CacheFactory.Cache.Remove(key);
            }
            return Success("清理成功");
        }
        #endregion
    }
}